//
//  CurtainView.m
//  Test
//
//  Created by jingyao on 2017/12/22.
//  Copyright © 2017年 jingyao. All rights reserved.
//

#import "CurtainView.h"
#import "CurtainControllView.h"
#import "Global.h"

@interface CurtainView()

@property (weak, nonatomic) IBOutlet UIView *controllContentView;
@property (weak, nonatomic) IBOutlet UILabel *changeStateLbl;
@property (strong, nonatomic) CurtainControllView *controlView;

@end

@implementation CurtainView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.changeStateLbl.text = @"Move to control";

    //获取通知中心单例监听通知事件
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(receiveNotificationValue:) name:@"notificationName" object:nil];
    
    self.controlView = [CurtainControllView createViewFromXIB];
    
    __weak typeof(self) weakSelf = self;
    self.controlView.changedRateBlock = ^(CGFloat rate) {
        weakSelf.changeStateLbl.text = @"Move to control";
    };
    
    self.controlView.changingUpRateBlock = ^(BOOL isUp) {
        if (isUp) {
            weakSelf.changeStateLbl.text = @"Moving down...";
        } else {
            weakSelf.changeStateLbl.text = @"Moving up...";
        }
    };
    self.controlView.frame = self.controllContentView.bounds;
    [self.controllContentView addSubview:self.controlView];
    
    [self.controlView setTranslatesAutoresizingMaskIntoConstraints:NO];
    
    [self.controllContentView addConstraint:[NSLayoutConstraint constraintWithItem:self.controllContentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.controlView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
    [self.controllContentView addConstraint:[NSLayoutConstraint constraintWithItem:self.controllContentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.controlView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
    [self.controllContentView addConstraint:[NSLayoutConstraint constraintWithItem:self.controllContentView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.controlView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
    [self.controllContentView addConstraint:[NSLayoutConstraint constraintWithItem:self.controllContentView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.controlView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];
}

- (void)receiveNotificationValue:(NSNotification *)notification {
    NSLog(@"监听通知中心的selector方法调用了");
    NSInteger value = [notification.object integerValue];
    CGFloat rate = value/99.0f;
    cRate = rate;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"notificationName" object:nil];
}

@end
